package com.example.service.impl;

import com.example.dao.GoodsDao;
import com.example.entity.Goods;
import com.example.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public Goods getGoodsById(Integer gid) {
        return goodsDao.findById(gid);
    }

    @Override
    public List<Goods> getAllGoods() {
        return goodsDao.findAll();
    }

    @Override
    public boolean addGoods(Goods goods) {
        return goodsDao.insert(goods) > 0;
    }

    @Override
    public boolean updateGoods(Goods goods) {
        return goodsDao.update(goods) > 0;
    }

    @Override
    public boolean deleteGoods(Integer gid) {
        return goodsDao.delete(gid) > 0;
    }

    @Override
    public int totalPage(int pageNum) {
        int totalNum=this.goodsDao.findCount();
        int totalPage=(int)Math.ceil(totalNum/(double)pageNum);
        return totalPage;
    }

    @Override
    public List<Goods> findGoodsByPage(int page,int pageNum) {
        List<Goods> goodsList = this.goodsDao.findGoodsByPage((page-1)*pageNum,pageNum);
        return goodsList;
    }

    @Override
    public List<Goods> getGoodsByAuthor(String name) {
        List<Goods> goodsList = this.goodsDao.findGoodsByAuthor(name);
        return goodsList;
    }

    @Override
    public List<Goods> searchGoodsByType(String type,String author) {
        return goodsDao.searchGoodsByType(type,author);
    }
}
